package com.remiss.seckilldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.remiss.seckilldemo.exception.GlobalExecption;
import com.remiss.seckilldemo.mapper.OrderMapper;
import com.remiss.seckilldemo.pojo.Order;
import com.remiss.seckilldemo.pojo.SeckillGoods;
import com.remiss.seckilldemo.pojo.SeckillOrder;
import com.remiss.seckilldemo.pojo.User;
import com.remiss.seckilldemo.service.IGoodsService;
import com.remiss.seckilldemo.service.IOrderService;
import com.remiss.seckilldemo.service.ISeckillGoodsService;
import com.remiss.seckilldemo.service.ISeckillOrderService;
import com.remiss.seckilldemo.vo.GoodsVO;
import com.remiss.seckilldemo.vo.OrderDetailVO;
import com.remiss.seckilldemo.vo.ResposeBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author remiss
 * @since 2021-04-03
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Transactional
    @Override
    public Order seckill(User user, GoodsVO goodsVO) {
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id", goodsVO.getId()));
        seckillGoods.setStockCount(seckillGoods.getStockCount()-1);
//        seckillGoodsService.updateById(seckillGoods);
        boolean result = seckillGoodsService.update(new UpdateWrapper<SeckillGoods>()
                .setSql("stock_count = stock_count -1")
                .eq("id", seckillGoods.getId())
                .gt("stock_count", 0));
        if (!result){
            return null;
        }
        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goodsVO.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(goodsVO.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(goodsVO.getSeckillPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderMapper.insert(order);
//        生成秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(user.getId());
        seckillOrder.setGoodsId(goodsVO.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrderService.save(seckillOrder);
        redisTemplate.opsForValue().set("order:"+user.getId() +":" +goodsVO.getId(),seckillOrder);
        redisTemplate.opsForValue().decrement("goods:" +goodsVO.getId());
        return order;
    }

    @Override
    public OrderDetailVO detail(Long orderId) {
        if (orderId ==null){
            throw new GlobalExecption(ResposeBeanEnum.NO_ORDER_ERROR);
        }
        Order order = orderMapper.selectById(orderId);
        if(null == order){
            throw new GlobalExecption(ResposeBeanEnum.NO_ORDER_ERROR);
        }
        GoodsVO goodsVO = goodsService.findGoodsVOByGoodsId(order.getGoodsId());
        OrderDetailVO detailVO= new OrderDetailVO(order,goodsVO);
        return detailVO;
    }
}
